<template>
	<view>
		<!-- native标志为当前页面 -->
		<my-search @click="goSearch"></my-search>
		<view class="scroll-view-container">
			<!-- 左侧滚动区域 -->
			<scroll-view scroll-y="true" :style="{height:wh+'px'}" class="left-item-list">
				<block v-for="(item,index) in cateList" :key="index">
					<view :class="['left-item',index===active?'active':'']" @click="activeHandler(index)">
						{{item.cat_name}}
					</view>
				</block>

			</scroll-view>
			<!-- 右侧滚动区域 -->
			<scroll-view scroll-y="true" :style="{height:wh+'px'}" class="right-item-list" :scroll-top="scrollTop">
				<view class="right-item" v-for="(item,index) in cateLevel2" :key="index">
					<!-- 先渲染名称 -->
					<view class=" level-title">{{item.cat_name}}</view>
					<view class="cate-lv-item">
						<!-- 二次循环 -->
						<view class="item-level" v-for="(item1,index1) in item.children" :key="index1"
							@click="gotoGoodsList(item1)">

							<image class=" item-level-img" :src="item1.cat_icon.replace('ugo-dev','hmugo-web')"
								mode="widthFix">
							</image>
							<text>{{item1.cat_name}}</text>

						</view>

					</view>
				</view>

			</scroll-view>
		</view>
	</view>
</template>

<script>
	import badgeMix from '@/mixins/tabbar-badge.js'
	export default {
		mixins: [badgeMix],
		data() {
			return {
				wh: 0,
				cateList: [],
				active: 0,
				cateLevel2: [],
				scrollTop: 0

			};

		},

		onLoad() {

			const msg = uni.getSystemInfoSync()
			this.wh = msg.windowHeight - 50
			this.getcateList()

		},
		methods: {
			async getcateList() {

				const res = await uni.$http.get('/api/public/v1/categories')

				if (res.data.meta.status !== 200) {
					return uni.$showMsg("数据请求失败")
				}
				this.cateList = res.data.message
				this.cateLevel2 = res.data.message[0].children
				console.log(this.cateLevel2)

			},
			activeHandler(index) {
				this.active = index
				this.cateLevel2 = this.cateList[index].children
				// 一像素的偏差 前后赋值不能相同
				this.scrollTop = Math.random()
			},
			gotoGoodsList(item) {
				uni.navigateTo({
					url: '/subpackage/goods_list/goods_list?cid=' + item.cat_id
				})
			},
			goSearch() {
				uni.navigateTo({
					url: "/subpackage/search/search"
				})
			},

		}
	}
</script>

<style lang="scss">
	.scroll-view-container {
		display: flex;
	}

	.left-item-list {
		width: 120px;
	}

	.left-item {
		background-color: #f7f7f7;
		line-height: 60px;
		text-align: center;
		font-size: 12px;

		&.active {
			background-color: #ffffff;
			position: relative;

			&::before {
				content: "";
				display: block;
				width: 3px;
				height: 30px;
				background-color: red;
				position: absolute;
				left: 0;
				top: 30%;
			}
		}
	}



	.level-title {
		font-size: 12px;
		text-align: center;
		padding: 15px 0;
	}

	.cate-lv-item {

		display: flex;
		flex-wrap: wrap;

	}

	.item-level-img {
		width: 80px;
		height: 80px;
		margin-top: 10px;
	}

	.item-level {
		width: 33.3%;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		margin-bottom: 10px;
	}
</style>